package com.ruoyi.common.customize.redis.config;

import lombok.AllArgsConstructor;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson配置
 *
 * @author gdk
 * @since 2022/9/19 19:53
 */
@Configuration
@AllArgsConstructor
public class RedissonConfig {

    private final RedisCustomizeProperties redisCustomizeProperties;

    /**
     * 对 Redisson 的使用都是通过 RedissonClient 对象
     *
     * @return {@link RedissonClient}
     * @author gdk
     * @since 2022/9/19 20:04
     */
    @Bean(destroyMethod = "shutdown") // 服务停止后调用 shutdown 方法。
    public RedissonClient redisson() {
        String addressFormat = "redis://%s:%s";

        // 1.创建配置
        Config config = new Config();
        // 集群模式
        // config.useClusterServers().addNodeAddress("127.0.0.1:7004", "127.0.0.1:7001");
        // 2.根据 Config 创建出 RedissonClient 示例。
        String address = String.format(addressFormat, redisCustomizeProperties.getHost(), redisCustomizeProperties.getPort());
        config.useSingleServer()
                .setAddress(address)
                .setPassword(redisCustomizeProperties.getPassword())
                .setDatabase(redisCustomizeProperties.getDatabase());

        return Redisson.create(config);
    }
}
